<template>
  <p-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <p-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <p-form :form="form">
        <p-row>

	  <p-col :span="12">
		<p-form-item label="主键" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'id', validatorRules.id]" placeholder="请输入主键"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="父级节点" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'pid', validatorRules.pid]" placeholder="请输入父级节点"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="工序编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'processmcode', validatorRules.processmcode]" placeholder="请输入工序编码"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="工序名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'processmname', validatorRules.processmname]" placeholder="请输入工序名称"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="工序描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'processsprc', validatorRules.processsprc]" placeholder="请输入工序描述"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="工序层级" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'processlayer', validatorRules.processlayer]" placeholder="请输入工序层级"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="产品类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'producttype', validatorRules.producttype]" placeholder="请输入产品类型"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="偏置期（天）" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'productffset', validatorRules.productffset]" placeholder="请输入偏置期（天）"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="合并" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'distribution', validatorRules.distribution]" placeholder="请输入合并"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'status', validatorRules.status]" placeholder="请输入状态"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="技能" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'skill', validatorRules.skill]" placeholder="请输入技能"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="资源" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'resource', validatorRules.resource]" placeholder="请输入资源"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="工序BOM" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'processbom', validatorRules.processbom]" placeholder="请输入工序BOM"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="SOP" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'sop', validatorRules.sop]" placeholder="请输入SOP"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="线材清单" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'wirerod', validatorRules.wirerod]" placeholder="请输入线材清单"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="组件清单" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'module', validatorRules.module]" placeholder="请输入组件清单"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="耗损系数" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'consume', validatorRules.consume]" placeholder="请输入耗损系数"></p-input>
		</p-form-item>
      </p-col>
	  <p-col :span="12">
		<p-form-item label="程序" :labelCol="labelCol" :wrapperCol="wrapperCol">
			<p-input   v-decorator="[ 'gram', validatorRules.gram]" placeholder="请输入程序"></p-input>
		</p-form-item>
      </p-col>

        </p-row>
      </p-form>

      <!-- 子表单区域 -->
      <p-tabs v-model="activeKey" @change="handleChangeTabs">
        <p-tab-pane tab="工序定义技能" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="mesPeSkillskillTable.loading"
            :columns="mesPeSkillskillTable.columns"
            :dataSource="mesPeSkillskillTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </p-tab-pane>
        
        <p-tab-pane tab="资源" :key="refKeys[1]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[1]"
            :loading="mesPeResourceTable.loading"
            :columns="mesPeResourceTable.columns"
            :dataSource="mesPeResourceTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </p-tab-pane>
        
      </p-tabs>

    </p-spin>
  </p-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'MesPeDefinitionModal',
    mixins: [JEditableTableMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 16
        },
        labelCol2: {
          span: 3
        },
        wrapperCol2: {
          span: 20
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          pid: {rules: [
            {required: true, message: '请输入父级节点!'},
          ]},
          processmcode: {rules: [
            {required: true, message: '请输入工序编码!'},
          ]},
          processmname: {rules: [
            {required: true, message: '请输入工序名称!'},
          ]},
          processsprc: {rules: [
            {required: true, message: '请输入工序描述!'},
          ]},
          processlayer: {rules: [
            {required: true, message: '请输入工序层级!'},
          ]},
          producttype: {rules: [
            {required: true, message: '请输入产品类型!'},
          ]},
          productffset: {rules: [
            {required: true, message: '请输入偏置期（天）!'},
          ]},
          distribution: {rules: [
          ]},
          status: {rules: [
          ]},
          skill: {rules: [
          ]},
          resource: {rules: [
          ]},
          processbom: {rules: [
          ]},
          sop: {rules: [
          ]},
          wirerod: {rules: [
          ]},
          module: {rules: [
          ]},
          consume: {rules: [
          ]},
          gram: {rules: [
          ]},
        },
        refKeys: ['mesPeSkillskill', 'mesPeResource', ],
        tableKeys:['mesPeSkillskill', 'mesPeResource', ],
        activeKey: 'mesPeSkillskill',
        // 工序定义技能
        mesPeSkillskillTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '技能代码',
              key: 'skillcode',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '技能名称',
              key: 'skillname',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '技能描述',
              key: 'skillsprc',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
          ]
        },
        // 资源
        mesPeResourceTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '资源类型',
              key: 'resourcetype',
              type: FormTypes.select,
              dictCode: 'mes_ba_resource',
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '资源名称',
              key: 'resourcename',
              type: FormTypes.select,
              dictCode: 'zymc',
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '资源规格型号',
              key: 'resourceSpecification',
              type: FormTypes.select,
              dictCode: 'zyggxh',
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '数量',
              key: 'qty',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: 'id外键',
              key: 'mesBaProcessdefinitionId',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        url: {
          add: '/mes-craft/auto/mesPeDefinition/add',
          edit: '/mes-craft/auto/mesPeDefinition/edit',
          mesPeSkillskill: {
            list: '/mes-craft/auto/mesPeDefinition/queryMesPeSkillskillByMainId'
          },
          mesPeResource: {
            list: '/mes-craft/auto/mesPeDefinition/queryMesPeResourceByMainId'
          },
        }
      }
    },
    methods: {
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'createBy','createTime','updateBy','updateTime','pid','processmcode','processmname','processsprc','processlayer','producttype','productffset','distribution','status','skill','resource','processbom','sop','wirerod','module','consume','gram')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.mesPeSkillskill.list, params, this.mesPeSkillskillTable)
          this.requestSubTableData(this.url.mesPeResource.list, params, this.mesPeResourceTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          mesPeSkillskillList: allValues.tablesValue[0].values,
          mesPeResourceList: allValues.tablesValue[1].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'createBy','createTime','updateBy','updateTime','pid','processmcode','processmname','processsprc','processlayer','producttype','productffset','distribution','status','skill','resource','processbom','sop','wirerod','module','consume','gram'))
     },

    }
  }
</script>

<style scoped>
</style>